<template>
  <div class="app-container">
    <el-card>
      <el-form :model="list_q" ref="list_q_ref" inline>
        <el-form-item
          v-for="(
            { form_item_prop: { label, prop }, component_name, component_prop },
            index
          ) in list_q_c"
          :key="index"
          v-bind="{ label, prop }"
        >
          <component
            :is="component_name"
            v-bind="component_prop"
            :placeholder="label"
            v-model="list_q[prop]"
          ></component>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="get_list">搜索</el-button>
          <el-button @click="reset('list_q_ref')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="open_add_dialog">新增</el-button>
      <el-table :data="list">
        <el-table-column type="selection"></el-table-column>
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column label="操作" width="200px" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="open_edit_dialog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="del_row(row)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="
          (val) => {
            (pager.limit = val), get_list();
          }
        "
        @current-change="
          (val) => {
            (pager.page = val), get_list();
          }
        "
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :visible.sync="visible"
      :title="title"
      @close="reset('list_e_ref')"
      center
    >
      <el-form :model="list_e" ref="list_e_ref" inline>
        <el-form-item
          v-for="(
            {
              form_item_prop: { label, prop, required },
              component_name,
              component_prop,
              handler,
            },
            index
          ) in list_e_c"
          :key="index"
          v-bind="{ label, prop, required }"
        >
          <component
            :is="component_name"
            v-bind="component_prop"
            v-model="list_e[prop]"
            :placeholder="label"
            v-on="handler"
          >
            <template v-if="component_name == 'el-select'">
              <el-option
                v-for="i in component_prop.option_data"
                :key="i.id"
                :value="i[component_prop.option_prop.value]"
                :label="i[component_prop.option_prop.label]"
              ></el-option>
            </template>
          </component>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { get_check_stock_list, get_tree, get_material, edit_check_stock, add_check_stock, del_check_stock, get_area, get_warehouse, get_loaction } from '@/api_zc/wms/check-stock'
import config_mixin from './config_mixin'
export default {
  mounted () {
    this.get_list()
    this.get_unity_list()
  },
  mixins: [config_mixin],
  methods: {
    async get_list () {
      if (!this.list_q.inventoryAttr) {
        return
      }
      if (!this.list_q.checkStockAttr) {
        return
      }
      [this.list_q.inventoryStartDate, this.list_q.inventoryEndDate] = this.list_q.inventoryAttr;
      [this.list_q.checkStockStartDate, this.list_q.checkStockEndDate] = this.list_q.checkStockAttr
      const { data: { content, total } } = await get_check_stock_list({ ...this.list_q, ...this.pager })
      this.list = content
      this.pager.total = total
    },
    reset (ref) {
      this.$refs[ref].resetFields()
      this.get_list()
    },
    del_row (row) {
      this.$confirm(`是否确认删除盘点信息编号为“${row.checkStockNo}”的数据项？`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await del_check_stock({ ids: [row.id] })
        this.$message.success('删除成功')
        this.get_list()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    open_edit_dialog (row) {
      this.materialChange2(row.material.id, row)
      this.visible = true
      this.title = '修改盘点信息'
      this.is_add = false
      this.$nextTick(() => {
        Object.assign(this.list_e, {
          ...row,
          materialId: row.material.id
        })
      })
    },
    open_add_dialog () {
      this.is_add = true
      this.visible = true
      this.title = '添加盘点信息'
    },
    async get_unity_list () {
      const { data } = await get_material()
      this.list_e_c.find(item => item.form_item_prop.label == '物料').component_prop.option_data = data
    },
    async materialChange (materialId) {
      const { data } = await get_tree({ materialId })
      this.list_e_c.find(item => item.form_item_prop.label == '仓库/库区/库位').component_prop.key++
      this.list_e_c.find(item => item.form_item_prop.label == '仓库/库区/库位').component_prop.options = data
      this.list_e.attr = [data[0].id, data[0].children[0].id, data[0].children[0].children[0].id]
    },
    async materialChange2 (materialId, row) {
      const { data } = await get_tree({ materialId })
      this.list_e_c.find(item => item.form_item_prop.label == '仓库/库区/库位').component_prop.key++
      this.list_e_c.find(item => item.form_item_prop.label == '仓库/库区/库位').component_prop.options = data
      this.list_e.attr = [row.warehouse.id, row.area.id, row.location.id]
    },
    submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善表单')
        }
        [this.list_e.warehouseId, this.list_e.areaId, this.list_e.locationId] = this.list_e.attr
        if (this.is_add) {
          await add_check_stock(this.list_e)
          this.$message.success('添加成功')
        } else {
          await edit_check_stock(this.list_e)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.get_list()
      })
    }
  },
}
</script>

<style>
</style>